/**
 * 
 */
package com.showtao.front.service;

import java.text.DecimalFormat;
import java.util.Calendar;

import org.apache.cxf.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.showtao.common.enumeration.EErrorCode;
import com.showtao.common.exception.BizServiceException;
import com.showtao.front.domain.MobileUser;
import com.showtao.front.domain.OrderValidationCode;
import com.showtao.front.repository.MobileUserRepository;
import com.showtao.front.repository.OrderValidationCodeRepository;

/**
 * @author Fanxin Zeng
 *
 */
@Service
public class MobileUserService {
	@Autowired
	MobileUserRepository mobileUserRepository;
	@Autowired
    private OrderValidationCodeRepository orderValidationCodeRepository;
    @Autowired
    private SMSService smsService;

    @Transactional
    public void sendValidationCode(String mobile){
        if (StringUtils.isEmpty(mobile)) {
            throw new BizServiceException(EErrorCode.COMM_INTERNAL_ERROR,
                    "参数不正确");
        }
        OrderValidationCode ovc = orderValidationCodeRepository.getByMobile(mobile);
        if(ovc == null){
            ovc = new OrderValidationCode();
            ovc.setCreatedAt(Calendar.getInstance().getTime());
            ovc.setMobile(mobile);
        }
        DecimalFormat df = new DecimalFormat("0000");
        String code = df.format(Math.round(Math.random()*1000));
        smsService.sendMessage(mobile, "秀套网验证码：【"+code+"】，半小时内有效，打死都不告诉别人哦！");
        ovc.setCode(code);
        ovc.setUpdatedAt(Calendar.getInstance().getTime());
        orderValidationCodeRepository.save(ovc);
    }
    
    @Transactional
    public void validateCode(String mobile, String code){
        if (StringUtils.isEmpty(code)) {
            throw new BizServiceException(EErrorCode.COMM_INTERNAL_ERROR,
                    "参数不正确");
        }
        OrderValidationCode ovc = orderValidationCodeRepository.getByMobile(mobile);
        if(ovc==null){
            throw new BizServiceException(EErrorCode.COMM_INTERNAL_ERROR, "验证码"+code+"无效");
        }else if(StringUtils.isEmpty(ovc.getCode()) || ovc.getUpdatedAt() == null){
            throw new BizServiceException(EErrorCode.COMM_INTERNAL_ERROR, "验证码"+code+"已失效");
        }else if(!code.equals(ovc.getCode())){
            throw new BizServiceException(EErrorCode.COMM_INTERNAL_ERROR, "验证码"+code+"不正确");
        }else {
            Calendar c = Calendar.getInstance();
            c.setTime(ovc.getUpdatedAt());
            c.add(Calendar.MINUTE, 30);
            if(c.before(Calendar.getInstance())){
                throw new BizServiceException(EErrorCode.COMM_INTERNAL_ERROR, "验证码"+code+"已过期");
            }
        }
        ovc.setCode(null);
        ovc.setUpdatedAt(Calendar.getInstance().getTime());
        orderValidationCodeRepository.save(ovc);
    }
    
    @Transactional
    public void registerMobileUser(MobileUser mobileUser){
    	mobileUserRepository.save(mobileUser);
    }
    
    public MobileUser getByMobile(String mobile){
    	return mobileUserRepository.getByMobile(mobile);
    }
    
    @Transactional
    public void saveMobileUser(MobileUser mobileUser){
    	mobileUserRepository.save(mobileUser);
    }
}
